package com.ja.openchat.views.banner;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.ja.openchat.R;
import com.zy.openchat.core.model.photo.PhotoInfo;
import com.zy.openchat.core.model.video.VideoInfo;

public class ImageRoundHolderCreator implements HolderCreator {
    @Override
    public View createView(final Context context, final int index, Object o) {
        View inflate = View.inflate(context, R.layout.item_round_image, null);
        ImageView imageView = inflate.findViewById(R.id.img);
        View playImageView = inflate.findViewById(R.id.playImageView);
        playImageView.setVisibility(View.GONE);
        if(o!=null){
            if(o instanceof PhotoInfo){
                PhotoInfo photoInfo = (PhotoInfo) o;
                if(photoInfo.isPhotoDownloaded()){
                    Glide.with(context)
                            .load(photoInfo.getPhotoImagePath())
                            .into(imageView);
                }else{
                    imageView.setImageResource(R.drawable.image_chat_placeholder_white);
                }
            }else if(o instanceof VideoInfo){
                VideoInfo videoInfo = (VideoInfo) o;
                if(videoInfo.isVideo()){
                    RequestBuilder<Drawable> thumbnailRequest = null;
                    if(videoInfo.thumbnail!=null&&videoInfo.thumbnail.isThumbnailDownloaded()){
                        thumbnailRequest = Glide
                                .with(context)
                                .load(videoInfo.thumbnail.localPath());
                    }else{
                        thumbnailRequest = Glide
                                .with(context)
                                .load(R.drawable.image_chat_placeholder_white);
                    }
                    Glide.with(context)
                            .load(videoInfo.localVideoPath())
                            .thumbnail(thumbnailRequest)
                            .into(imageView);

                }if(videoInfo.isAnimationDownloaded()){
                    Glide.with(context)
                            .load(videoInfo.localAnimationPath())
                            .into(imageView);
                }else{
                    imageView.setImageResource(R.drawable.image_chat_placeholder_white);
                }
                playImageView.setVisibility(videoInfo.isVideo()?View.VISIBLE:View.GONE);
            }else{
                imageView.setImageResource(R.drawable.image_chat_placeholder_white);
            }
        }
        return inflate;
    }
}
